package com.appspot.flipring;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ozonetel.kookoo.CollectDtmf;
import com.ozonetel.kookoo.Response;

public class PriceServlet extends HttpServlet {

	private static final long serialVersionUID = 6405416403272879573L;
	private static final String STATE = "state";
	private static final String VAL_STATE_SEARCH = "search";
	private static final String VAL_STATE_HELLO = "hello";

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		String state = (String) session.getAttribute(PriceServlet.STATE);
		Response responseXML = new Response();
		CollectDtmf collectDtmf = new CollectDtmf();
		if (state == null || state.equals(VAL_STATE_HELLO)) {
			collectDtmf.addPlayText("To know the price of a book, please enter the I S B N number of the book");
			session.setAttribute(STATE, VAL_STATE_SEARCH);
		} else if (state.equals(VAL_STATE_SEARCH)) {
			Book book = new Book(req.getParameter("data"));
			collectDtmf.addPlayText(book.bookName);
			collectDtmf.addPlayText(" is priced at rupees ");
			collectDtmf.addPlayText(book.price);
			collectDtmf.addPlayText(" after a discount of " + book.discount);
			collectDtmf.addPlayText("Thank you");
			session.setAttribute(STATE, VAL_STATE_HELLO);
		}

		responseXML.addCollectDtmf(collectDtmf);
		resp.getWriter().print(responseXML.getXML());

	}
}

class Book {
	String price;
	String bookName;
	String ISBN;
	String discount;

	public Book(String isbn) {
		URL url;
		try {
			String priceTag = "<span class=\"product_our_price\">Rs. ";
			String bookTag = "<meta name=\"og_title\" property=\"og:title\" content=\"";
			String discountTag = "<span class=\"product_you_save\">Rs. ";

			url = new URL("http://www.flipkart.com/search-book?dd=0&query=" + isbn);
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
			String line = null;
			while ((line = reader.readLine()) != null) {
				int start = line.indexOf(priceTag);
				if (start != -1) {
					this.price = line.substring(start + priceTag.length(), line.indexOf("</", start));
				}
				start = line.indexOf(discountTag);
				if (start != -1) {
					this.discount = line.substring(start + discountTag.length(), line.indexOf("</", start));
				}
				start = line.indexOf(bookTag);
				if (start != -1) {
					this.bookName = line.substring(start + bookTag.length(), line.indexOf("\"/>", start));
				}
			}
			reader.close();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}